/**
 * Created by liubin on 2014/07/03.
 */
'use strict';

/**
 * 定义requirejs的总模块
 */
define(['angular', 'angular-sanitize', 'angular-resource', 'ng-grid'], function (angular) {
    // 设定加载的angular 模块
    return angular.module('agcompany', [
            'ui.bootstrap',
            'ngSanitize',
            'ngRoute',
            'ngGrid',
            'agcompany.controllers',
            'agcompany.services'
        ]
//        // 解决与jinja2 模板的 冲突
//        function ($interpolateProvider, $locationProvider) {
//            $interpolateProvider.startSymbol('[[');
//            $interpolateProvider.endSymbol(']]');
//            // 路由 html5
//            $locationProvider.html5Mode(true);
//            $locationProvider.hashPrefix('!');
//        }
    ).config(["$interpolateProvider", "$locationProvider", function ($interpolateProvider, $locationProvider) {
            $interpolateProvider.startSymbol('[[');
            $interpolateProvider.endSymbol(']]');
            // 路由 html5
            $locationProvider.html5Mode(true);
            $locationProvider.hashPrefix('!');
        }]).run(["$rootScope", "$templateCache", "$interpolate", function ($rootScope, $templateCache, $interpolate) {
            $rootScope.$on("$routeChangeSuccess", function (scope, current) {
                var url = current.templateUrl ? current.templateUrl : current.redirectTo;
                console.log(url);
                switch (url) {
                    case urlRoute.getUrl('tpl_userlist'):
                    case urlRoute.getUrl('tpl_useredit'):
                        $rootScope.menu = {
                            userlist: 'active'
                        }
                        break;
                    case urlRoute.getUrl('tpl_rolelist'):
                        $rootScope.menu = {
                            rolelist: 'active'
                        }
                        break;
                    case urlRoute.getUrl('index'):
                        $rootScope.menu = {};
                        break;
                }
                var getFn = $templateCache.get;
                $templateCache.get = function (key) {
                    var result = getFn(key);
                    console.log(result);
                    if(result && !Array.isArray(result)){
                        //return result.replace(/{{/g, $interpolate.startSymbol).replace(/}}/g, $interpolate.endSymbol);
                        console.log(result.replace(/{{/g, $interpolate.startSymbol));
                        console.log(result.replace(/{{/g, $interpolate.startSymbol).replace(/}}/g, $interpolate.endSymbol));
                        console.log($interpolate.startSymbol);
                        console.log($interpolate.endSymbol);
                        return result.replace(/{{/g, $interpolate.startSymbol).replace(/}}/g, $interpolate.endSymbol);
                    }
                    else{
                        return result;
                    }
                }
            });
        }]);
});
